package com.hn.zhijian.system.rsasdk;

import org.apache.tomcat.util.codec.binary.StringUtils;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.MD2Digest;
import org.bouncycastle.crypto.digests.MD4Digest;
import org.bouncycastle.crypto.digests.MD5Digest;
import org.bouncycastle.util.encoders.Hex;

/**
 * Md5加密工具类
 *
 * @author LiuShaohai
 * @date 2018年7月31日
 * @filename Md5.java
 * @location
 * @tags
 */
public class Md5 extends AbstractSecurity {

	/**
	 * MD2加密
	 * 
	 * @param arg 明文串
	 * @return 密文串
	 * @since 2.0.1
	 */
	public String md2(String arg) {
		return encrypt(arg, new MD2Digest());
	}

	/**
	 * MD4加密
	 * 
	 * @param arg 明文串
	 * @return 密文串
	 * @since 2.0.1
	 */
	public String md4(String arg) {
		return encrypt(arg, new MD4Digest());
	}

	/**
	 * MD5加密
	 * 
	 * @param arg 明文串
	 * @return 密文串
	 * @since 2.0.1
	 */
	public String md5(String arg) {
		return encrypt(arg, new MD5Digest());
	}

	/**
	 * MD加密
	 * 
	 * @param arg 明文串
	 * @return 密文串
	 * @since 2.0.1
	 */
	private String encrypt(String arg, Digest digest) {
		digest.update(arg.getBytes(), 0, arg.getBytes().length);
		byte[] bytes = new byte[digest.getDigestSize()];
		digest.doFinal(bytes, 0);

		return StringUtils.newStringUtf8(Hex.encode(bytes));
	}
}